home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-06-20 | 9.7 KB | 444 lines | [TEXT/MPS ] |
- unit Utilities;
-
- interface
- uses Types, Memory, QuickDraw, Resources, Windows, Packages, ToolUtils;
-
- procedure GetGlobalRect (window: WindowPtr;
- var globalRect: Rect);
- procedure ForceOnScreen (aWindow: WindowPtr);
- procedure SetToZoomToBestMonitor (window: WindowPtr);
- function WindowZoomed (window: WindowPtr): boolean;
-
- { function CurrentMonitor (window: WindowPtr): gdHandle;}
-
- function Sign (aNumber: integer): integer;
- function Min (a, b: integer): integer;
- procedure ReasonableString (number: longint;
- var resultString: str255);
-
- procedure GenericForeColor (color: RGBColor);
- procedure TextBlack;
- procedure TextGray;
- procedure TextLightGray;
-
- procedure PenLightestGray;
- procedure PenLightGray;
- procedure PenGray;
- procedure PenBlack;
-
- function OneBitDeep: boolean;
- function HasColorQuickDraw: boolean;
- function HasSystemSevenOrBetter: boolean;
-
- procedure RememberWindows (theWindow, graphWindow: WindowPtr);
-
- implementation
-
- var
- bestDevice: GDHandle;
- bestArea: longint;
- globalWindowRect: Rect;
-
- procedure GetGlobalRect (window: WindowPtr;
- var globalRect: Rect);
- var
- savePort: GrafPtr;
- begin {Return the portRect of window in global coordinates.}
- GetPort(savePort);
- SetPort(window); {so that the correct }
- globalRect := window^.portRect; { coordinate system is used}
- with globalRect do
- begin
- LocalToGlobal(topLeft);
- LocalToGlobal(botRight);
- end;
- SetPort(savePort);
- end; {GetGlobalRect}
-
- procedure IsThisTheBestDevice (targetDevice: GDHandle);
- var
- deviceRect: Rect;
- area: longint;
- begin
- deviceRect := targetDevice^^.gdRect;
- if SectRect(deviceRect, globalWindowRect, deviceRect) then
- with deviceRect do
- begin
- area := longint(right - left) * longint(bottom - top);
- if area > bestArea then
- begin
- bestArea := area;
- bestDevice := targetDevice;
- end;
- end;
- end;{IsThisTheBestDevice}
-
- procedure ForEachGraphicsDeviceDo (procedure proc (theDevice: GDHandle));
- var
- theGDevice: GDHandle;
- begin
- theGDevice := GetDeviceList;
- while theGDevice <> nil do
- begin
- { if theGDevice^^.devType = screenDevice then }
- proc(theGDevice);
- theGDevice := GetNextDevice(theGDevice);
- end;
- end;
-
- function TitleBarHeight (window: WindowPtr): integer;
- begin
- with windowPeek(window)^ do
- TitleBarHeight := contRgn^^.rgnBBox.top - strucRgn^^.rgnBBox.top;
- end;
-
- procedure ZoomRectForThisMonitorAndWindow (gdH: gdHandle;
- window: WindowPtr;
- var theRect: Rect);
- begin
- theRect := gdH^^.gdRect;
- if gdH = GetMainDevice then
- theRect.top := theRect.top + GetMBarHeight + TitleBarHeight(window)
- else
- theRect.top := theRect.top + TitleBarHeight(window);
- end;
-
- procedure SetToZoomToBestMonitor (window: WindowPtr);
- type
- WSDPtr = ^WStateData;
- WSDH = ^WSDPtr;
- var
- bestRect: Rect;
- stateHandle: Handle;
- begin{SetToZoomToBestMonitor}
- GetGlobalRect(window, globalWindowRect);
- bestDevice := GetMainDevice;
- bestArea := 0;
- ForEachGraphicsDeviceDo(IsThisTheBestDevice);
- ZoomRectForThisMonitorAndWindow(bestDevice, window, bestRect);
- InsetRect(bestRect, 3, 3);
- stateHandle := WindowPeek(window)^.dataHandle;
- WSDH(stateHandle)^^.stdState := bestRect;
- end; {SetToZoomToBestMonitor}
-
- function WindowZoomed (window: WindowPtr): boolean;
- type
- WSDPtr = ^WStateData;
- WSDH = ^WSDPtr;
- var
- bestRect, currentRect: Rect;
- stateHandle: Handle;
- begin{SetToZoomToBestMonitor}
- GetGlobalRect(window, globalWindowRect);
- bestDevice := GetMainDevice;
- bestArea := 0;
- ForEachGraphicsDeviceDo(IsThisTheBestDevice);
- ZoomRectForThisMonitorAndWindow(bestDevice, window, bestRect);
- InsetRect(bestRect, 3, 3);
- stateHandle := WindowPeek(window)^.dataHandle;
- currentRect := WSDH(stateHandle)^^.stdState;
- currentRect := window^.portRect;
- LocalToGlobal(currentRect.topLeft);
- LocalToGlobal(currentRect.botRight);
- WindowZoomed := EqualRect(currentRect, bestRect);
- end;
-
- procedure ForceOnScreen (aWindow: WindowPtr);
- const
- grayRgnLoc = $9EE;
- type
- grayPtrType = ^RgnHandle;
- grayHandleType = ^grayPtrType;
- var
- windowRect: rect;
- grayRgnHandle: RgnHandle;
- grayHandle: grayPtrType;
- wMgr: WindowPtr;
- begin
- windowRect := aWindow^.portRect;
- SetPort(aWindow);
- LocalToGlobal(windowRect.topLeft);
- LocalToGlobal(windowRect.botRight);
- grayHandle := pointer(grayRgnLoc);
- grayRgnHandle := grayHandle^;
-
- if not RectInRgn(windowRect, grayRgnHandle) then
- begin
- GetWMgrPort(wMgr);
- SetPort(wMgr);
- InvertRect(windowRect);
- MoveWindow(aWindow, 10, 60, TRUE);
- SetPort(aWindow);
- end;
- end;
-
-
- function Sign (aNumber: integer): integer;
- begin
- if aNumber = 0 then
- Sign := 0
- else if aNumber < 0 then
- Sign := -1
- else
- Sign := 1;
- end;
-
- function Min (a, b: integer): integer;
- begin
- if a < b then
- Min := a
- else
- Min := b;
- end;
-
- procedure ReasonableString (number: longint;
- var resultString: str255);
- var
- numer, denom: longint;
- denomStr: str255;
- begin
- if number > 1000000 then
- begin
- numer := number div 1000000;
- denom := number - numer * 1000000;
- NumToString(denom div 100000, denomStr);
- NumToString(numer, resultString);
- if denom div 100000 = 0 then
- resultString := concat(resultString, 'M')
- else
- resultString := concat(resultString, '.', denomStr, 'M');
- end
- else if number > 10000 then
- begin
- numer := number div 1000;
- denom := number - numer * 1000;
- NumToString(denom div 100, denomStr);
- NumToString(numer, resultString);
- if denom div 100 = 0 then
- resultString := concat(resultString, 'K')
- else
- resultString := concat(resultString, '.', denomStr, 'K');
- end
- else
- begin
- NumToString(number, resultString);
- end;
- end;
-
- procedure GenericForeColor (color: RGBColor);
- begin
- {If we’ve got CQD, then let it map a light color to white}
- if HasColorQuickDraw and not OneBitDeep then
- RGBForeColor(color)
- else if bitor(bitand(longint(color.red), maxint), maxint + 1) > 32767 then
- ForeColor(whiteColor)
- else
- ForeColor(blackColor);
- end;
-
- procedure TextLightGray;
- var
- color: RGBColor;
- begin
- with color do
- begin
- red := 50000;
- green := 50000;
- blue := 50000;
- end;
- GenericForeColor(color);
- end;
-
- procedure TextGray;
- var
- color: RGBColor;
- begin
- with color do
- begin
- red := 32000;
- green := 32000;
- blue := 32000;
- end;
- GenericForeColor(color);
- end;
-
- procedure TextBlack;
- begin
- ForeColor(blackColor);
- end;
-
- procedure PenLightestGray;
- var
- color: RGBColor;
- begin
- with color do
- begin
- red := $FFF0;
- green := $FFF0;
- blue := $FFF0;
- end;
- if OneBitDeep then
- begin
- PenNormal;
- ForeColor(whiteColor);
- BackColor(whiteColor);
- end
- else
- GenericForeColor(color);
- end;
-
- procedure PenLightGray;
- var
- color: RGBColor;
- begin
- with color do
- begin
- red := 50000;
- green := 50000;
- blue := 50000;
- end;
- if OneBitDeep then
- begin
- PenNormal;
- ForeColor(whiteColor);
- end
- else
- RGBForeColor(color);
- end;
-
- procedure PenGray;
- var
- color: RGBColor;
- begin
- with color do
- begin
- red := 32000;
- green := 32000;
- blue := 32000;
- end;
- if OneBitDeep then
- begin
- PenNormal;
- PenPat(gray);
- end
- else
- RGBForeColor(color);
- TextMode(srcOr);
- end;
-
- procedure PenBlack;
- begin
- ForeColor(blackColor);
- TextMode(srcOr);
- PenPat(black);
- end;
-
- function OneBitDeep: boolean;
- var
- theRect: rect;
- deviceHandle: gdHandle;
- begin
- OneBitDeep := true;
- if not HasColorQuickDraw then
- exit(OneBitDeep);
- theRect := thePort^.portRect;
- deviceHandle := GetMaxDevice(theRect);
- if deviceHandle <> nil then
- OneBitDeep := deviceHandle^^.gdPMap^^.pixelSize <= 2;
- end;
-
- function SimpleGestalt (selector: OSType;
- var response: LongInt): OSErr;
- external;
-
- const
- gestaltQuickDrawVersion = 'qd ';
- gestalt8BitQD = $100;
- function HasColorQuickDraw: boolean;
- var
- err: integer;
- result: longint;
- begin
- err := SimpleGestalt(gestaltQuickDrawVersion, result);
- HasColorQuickDraw := (result >= gestalt8BitQD);
- end;
-
- const
- gestaltSystemVersion = 'sysv';
- function HasSystemSevenOrBetter: boolean;
- var
- err: integer;
- result: longint;
- begin
- err := SimpleGestalt(gestaltSystemVersion, result);
- HasSystemSevenOrBetter := (LoWord(result) >= $700);
- end;
-
-
- {Take aWindow and modify it's corresponding WIND resource so we can open it in the same size and position on next launch}
- procedure RememberWindow (aWindow: WindowPtr;
- resID: integer);
- type
- windType = record
- boundsRect: rect;
- procId: integer;
- visible: byte;
- filler1: byte;
- goAway: byte;
- filler2: byte;
- refCon: longint;
- title: str255;
- end;
- windTypePtr = ^windType;
- windTypeHandle = ^windTypePtr;
- var
- myWind: windTypeHandle;
- begin
- SetPort(aWindow);
- myWind := windTypeHandle(Get1Resource('WIND', resID));
- if myWind <> nil then
- begin
- myWind^^.boundsRect := aWindow^.portRect;
- LocalToGlobal(myWind^^.boundsRect.topLeft);
- LocalToGlobal(myWind^^.boundsRect.botRight);
- if WindowPeek(aWindow)^.visible then
- INTEGER(myWind^^.visible) := $0101
- else
- INTEGER(myWind^^.visible) := $0001;
- ChangedResource(Handle(myWind));
- WriteResource(Handle(myWind));
- ReleaseResource(Handle(myWind));
- end;
- end;
-
- procedure RememberWindows (theWindow, graphWindow: WindowPtr);
- begin
- RememberWindow(theWindow, 128);
- RememberWindow(graphWindow, 129);
- end;
-
- procedure DrawGrayRamp;
- var
- i: integer;
- color: RGBColor;
- begin
- SetPort(FrontWindow);
- for i := 1 to 256 do
- begin
- with color do
- begin
- red := i * 256;
- blue := red;
- green := red;
- end;
- PenSize(2, 2);
- RGBForeColor(color);
- MoveTo(i * 2, 0);
- LineTo(i * 2, thePort^.portRect.bottom);
- end;
- repeat
- until button;
- end;
-
-
- end.